package com.wjggy.core.common.page;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletRequest;

import com.wjggy.core.common.regular.RegularUtil;


 /**
 *  <p>Description:  </p>
 *  <p>Copyright:Copyright (c) 2012  </p>
 *  <p>Company:wjggy  </p>
 *  @author xiaofan.yu  
 *  @date   2012-2-4 下午12:19:35    
 *  @project  wjggy
 *  @version  1.0.0
 *  @see   
 *  @since 1.6 
 */
public class PageUtil {
 
	public static Page createPage(ServletRequest request,String actionUrl,String... attrs){
		Page page=new Page();
		String pageSize=request.getParameter("pageSize");
		String pageNumber=request.getParameter("pageNo");
		 
		if(RegularUtil.regularNumber(pageSize, 9)){//字符长度1-9位
			page.setPageSize(Integer.parseInt(pageSize));
		}
		if(RegularUtil.regularNumber(pageNumber, 9)){//字符长度1-9位
			page.setPageNumber(Integer.parseInt(pageNumber));
		}
	 
 
		StringBuffer url=new StringBuffer(actionUrl); 
		url.append("?");
		for(String attr:attrs){
			String attrVal=request.getParameter(attr);
			if(attrVal!=null){
				url.append(attr);
				url.append("=");
				url.append(attrVal);
				url.append("&");
			}
		}
		page.setToUrl(url.toString());
		return page;
	} 
	public static void checkPage(Page page){
		int countPage=0;
		if(page.getTotalCount()>0){
			if(page.getTotalCount()%page.getPageSize()==0){
				countPage=page.getTotalCount()/page.getPageSize();
			}else{
				countPage=page.getTotalCount()/page.getPageSize()+1;
			}
		} 
		if(page.pageNumber>countPage){
			page.setPageNumber(countPage-1);
		}else if(page.pageNumber<1){
			page.setPageNumber(0);
		}
	}
}

